package com.davidhigenbottam.HeatMap.client;

import java.util.HashMap;

import com.davidhigenbottam.HeatMap.shared.Query;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DataSources {
	
	
	private VerticalPanel vPanel= new VerticalPanel();
	
	private static DataSources instance= null;
	private HashMap<String, RadioButton> radioButtons = new HashMap<String, RadioButton>();
	
	private DataSources(){
		
		
		for(String dsStr : Query.dataSources.keySet()){
			String desc= Query.dataSources.get(dsStr);
			
			RadioButton r= new RadioButton("data_sources",desc);
			radioButtons.put(dsStr, r);
			vPanel.add(r);
			//default
			if(dsStr.equals(Query.HIGHER_EDUCATION_ENROLLMENT))
				r.setValue(true);
		}
		

	}
	
	public static DataSources getInstance(){
		if(instance==null)
			instance= new DataSources();
		return instance;	
	}
	
	public void addClickHandlers(final QueryUI queryUI){
		for(RadioButton r : radioButtons.values()){
			r.addClickHandler(new ClickHandler() {
			    @Override
				public void onClick(ClickEvent event) {
			    	queryUI.dataSourceListChanged();	
				}
			});
		}
	}
	
	public String getDataSourceStr(){
		for(String key : radioButtons.keySet()){
			if(radioButtons.get(key).getValue()){
				return key;
			}
		}
		//this shouldn't happen
		return Query.DEFAULT_DATASOURCE;
	}	

	public Panel getPanel() {
		return vPanel;
	}

	

}
